package io.github.harmonly.skyblock.command;

import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.CommandPermission;
import dev.jorel.commandapi.arguments.PlayerArgument;
import io.github.harmonly.skyblock.manager.DataManager;
import org.bukkit.entity.Player;

public class OPCommand implements SBCommand {

    @Override
    public String name() {
        return "op";
    }

    @Override
    public void register() {
        new CommandAPICommand("op")
                .withPermission(CommandPermission.OP)
                .withArguments(new PlayerArgument("player"))
                .executes((sender, args) -> {
                    args.getOptional(0).ifPresent(obj -> {
                        Player player = (Player) obj;
                        player.setOp(true);
                        DataManager.getPlayerData(player).ifPresent(playerData -> {
                            playerData.setRank("ADMIN");
                        });
                    });
                })
                .register();
        new CommandAPICommand("deop")
                .withPermission(CommandPermission.OP)
                .withArguments(new PlayerArgument("player"))
                .executes((sender, args) -> {
                    args.getOptional(0).ifPresent(obj -> {
                        Player player = (Player) obj;
                        player.setOp(true);
                        DataManager.getPlayerData(player).ifPresent(playerData -> {
                            playerData.removeProperty("rank");
                            playerData.updateDisplay();
                        });
                    });
                })
                .register();
    }

    @Override
    public void unregister() {
        CommandAPI.unregister("op");
        CommandAPI.unregister("deop");
    }
}
